package com.example.gmallproduct.web;


import com.example.gmallproduct.VO.web.Catalog2Vo;
import com.example.gmallproduct.entity.CategoryEntity;
import com.example.gmallproduct.service.CategoryService;
import com.example.gmallproduct.service.impl.CategoryServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Locale;
import java.util.Map;

@Controller
@Slf4j
public class IndexController {

    @Autowired
    private CategoryService categoryService;
    @Autowired
    private CategoryServiceImpl categoryService1;


    @RequestMapping(value = {"/index.html", "/index"})
    public String index(HttpServletRequest request, Model model) {

        //加载根分类
        List<CategoryEntity> list = categoryService.listAllRoot();
        log.info("查出的根节点数量:  {} ", list.size());
        model.addAttribute("roots", list);
/*      thymeleaf默认会加上如下的前缀与后缀
        private String prefix = "classpath:/templates/";
        private String suffix = ".html";
*/
        return "index";
    }

    @RequestMapping("/index/catalog.json")
    @ResponseBody
    public Map<String, List<Catalog2Vo>> getIndexJson() {
        //吞吐量 271/sec
//        Map<String, List<Catalog2Vo>> list = categoryService.getCatalogJson();
        //吞吐量: 2/sec
//        Map<String, List<Catalog2Vo>> list = categoryService1.getCatalogJsonByDb1();
        //吞吐量: 31/sec
//        Map<String, List<Catalog2Vo>> list = categoryService1.getCatalogJsonByDb();
        Map<String, List<Catalog2Vo>> list = categoryService1.getCatalogJsonByRedisLock();
        return list;
    }

}
